package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.qc.CommonDefect;
import com.qidianit.mes.pro.mapper.CommonDefectMapper;
import com.qidianit.mes.pro.service.CommonDefectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 常见缺陷业务实现类
 * @Author: wang fei long
 * @Date: 2025/9/22 0022 - 14:49
 */
@Service
public class CommonDefectServiceImpl implements CommonDefectService {

    @Autowired
    private CommonDefectMapper commonDefectMapper;

    /**
     * 分页查询缺陷列表
     */
    @Override
    public Result<PageInfo<CommonDefect>> selectList(CommonDefect commonDefect, int pageNum, int pageSize) {
        try {
            // 开启分页
            PageHelper.startPage(pageNum, pageSize);
            // 执行查询
            List<CommonDefect> defectList = commonDefectMapper.selectList(commonDefect);
            // 封装分页信息
            PageInfo<CommonDefect> pageInfo = new PageInfo<>(defectList);
            return Result.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 新增缺陷记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result save(CommonDefect commonDefect) {
        try {
            // 参数校验
            if (commonDefect == null) {
                return Result.error("缺陷信息不能为空");
            }
            if (commonDefect.getDefectDescription() == null || commonDefect.getDefectDescription().trim().isEmpty()) {
                return Result.error("缺陷描述不能为空");
            }
            if (commonDefect.getDetectionItemType() == null || commonDefect.getDetectionItemType().trim().isEmpty()) {
                return Result.error("检测项类型不能为空");
            }
            if (commonDefect.getDefectLevel() == null || commonDefect.getDefectLevel().trim().isEmpty()) {
                return Result.error("缺陷等级不能为空");
            }
            if (commonDefect.getCreatorId() == null) {
                return Result.error("创建人ID不能为空");
            }
            // 设置更新人ID（新增时通常与创建人一致）
            if (commonDefect.getUpdatorId() == null) {
                commonDefect.setUpdatorId(commonDefect.getCreatorId());
            }

            // 执行新增
            int rows = commonDefectMapper.save(commonDefect);
            if (rows > 0) {
                return Result.success("新增成功");
            } else {
                return Result.error("新增失败，影响行数为0");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID更新缺陷记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateById(CommonDefect commonDefect) {
        try {
            // 参数校验
            if (commonDefect == null || commonDefect.getId() == null) {
                return Result.error("缺陷ID不能为空");
            }
            if (commonDefect.getUpdatorId() == null) {
                return Result.error("更新人ID不能为空");
            }

            // 执行更新
            int rows = commonDefectMapper.updateById(commonDefect);
            if (rows > 0) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败，未找到对应缺陷或数据未变更");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID删除缺陷（逻辑删除）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(Long id) {
        try {
            // 参数校验
            if (id == null) {
                return Result.error("缺陷ID不能为空");
            }

            // 执行删除
            int rows = commonDefectMapper.deleteById(id);
            if (rows > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败，未找到对应缺陷");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除缺陷（逻辑删除）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result batchDelete(Long[] ids) {
        try {
            // 参数校验
            if (ids == null || ids.length == 0) {
                return Result.error("请选择需要删除的缺陷");
            }

            // 执行批量删除
            int rows = commonDefectMapper.batchDelete(ids);
            if (rows > 0) {
                return Result.success("成功删除 " + rows + " 条记录");
            } else {
                return Result.error("删除失败，未找到对应缺陷");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    @Override
    public Result<CommonDefect> selectById(Long id) {
        return Result.success(commonDefectMapper.selectById(id));
    }
}
